//
//  PLISTArray_to_VariousArrays.cpp
//  sad
//
//  Created by Christopher Kemsley on 2/4/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#include <iostream>
#include "Codec.hpp"
#include "URL.hpp"
#include "PLISTParser.hpp"
#include "Log.hpp"
#include "Exception.hpp"
#include "Model.hpp"

using namespace Sim ;






#define	ViewT	PLISTParser::Array
#define	ModelT	std::vector<std::string>



struct	PLISTToStringVector : public aCodec < ModelT* , ViewT >
{
	PLISTToStringVector ()
	:	aCodec < ModelT* , ViewT > ()
		{ }
	
	virtual bool	runDecode		( Any const & anyView , Any const & anyModel )
		{
		ModelT*			output	=	anyModel.as < ModelT* >() ;
		ViewT const&	input	=	anyView.as  < ViewT >() ;
		
		ViewT::const_iterator	iHere	=	input.begin() ;
		ViewT::const_iterator	iEnd	=	input.end() ;
		
		while ( iHere != iEnd )
			{
			Any const & v = *(iHere++) ;
			
			output -> push_back ( v.as<std::string>() ) ;
			}
		
		return true ;
		}
} ;
PLISTToStringVector _PLISTToStringVector ;


#undef	ViewT
#undef	ModelT







#define	ViewT	PLISTParser::Array
#define	ModelT	std::vector<ModelVariant>



struct	PLISTToModelVariantVector : public aCodec < ModelT* , ViewT >
{
	PLISTToModelVariantVector ()
	:	aCodec < ModelT* , ViewT > ()
	{ }
	
	virtual bool	runDecode		( Any const & anyView , Any const & anyModel )
	{
	ModelT*			output	=	anyModel.as < ModelT* >() ;
	ViewT const&	input	=	anyView.as  < ViewT >() ;
	
	ViewT::const_iterator	iHere	=	input.begin() ;
	ViewT::const_iterator	iEnd	=	input.end() ;
	
	while ( iHere != iEnd )
		{
		Any const & v = *(iHere++) ;
		
		output -> push_back ( v.as<ModelVariant>() ) ;
		}
	
	return true ;
	}
} ;
PLISTToModelVariantVector _PLISTToModelVariantVector ;


#undef	ViewT
#undef	ModelT








#define	ViewT	PLISTParser::Array
#define	ModelT	std::vector<URL>



struct	PLISTToPathVector : public aCodec < ModelT* , ViewT >
{
	PLISTToPathVector ()
	:	aCodec < ModelT* , ViewT > ()
	{ }
	
	virtual bool	runDecode		( Any const & anyView , Any const & anyModel )
		{
		ModelT*			output	=	anyModel.as < ModelT* >() ;
		ViewT const&	input	=	anyView.as  < ViewT >() ;
		
		ViewT::const_iterator	iHere	=	input.begin() ;
		ViewT::const_iterator	iEnd	=	input.end() ;
		
		while ( iHere != iEnd )
			{
			Any const & v = *(iHere++) ;
			
			if ( v.type() == typeid(URL) )
				output -> push_back ( v.as<URL>() ) ;
			else if ( v.type() == typeid(std::string) )
				output -> push_back ( URL(v.as<std::string>()) ) ;
			else
				{
				ThrowException ( "PLISTToPathVector" , "Types of Path vector were incorrect.\n" , __FILE__,__LINE__ ) ;
				}
			}
		
		return true ;
		}
} ;
PLISTToPathVector _PLISTToPathVector ;

